package oj_hihocode;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by zhangyi on 2017/7/10.
 */
public class T1105 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        ArrayList<Integer> heap = new ArrayList<>();
        heap.add(0);//占用第0位
        int size = 0;
        for (int i = 0; i < N; i++) {
            String flag = in.next();
            if (flag.equals("A")) {
                int weight = in.nextInt();
                size++;
                heap.add(0);
                heap.set(size, weight);
                int curr_pos = size;
                while (curr_pos != 1 && heap.get(curr_pos / 2) < heap.get(curr_pos)) {
                    int tmp = heap.get(curr_pos);
                    heap.set(curr_pos, heap.get(curr_pos / 2));
                    heap.set(curr_pos / 2, tmp);
                    curr_pos = curr_pos / 2;
                }
            } else {
                System.out.println(heap.get(1));
                heap.set(1, heap.get(size));
                size--;
                int curr_pos = 1;
                while (curr_pos <= size / 2) {
                    //find max child
                    int maxChild;
                    if (curr_pos * 2 + 1 <= size) {
                        if (heap.get(curr_pos * 2 + 1) > heap.get(curr_pos * 2)) {
                            maxChild = curr_pos * 2 + 1;
                        } else {
                            maxChild = curr_pos * 2;
                        }
                    }else {
                        maxChild = curr_pos * 2;
                    }

                    //compare and exchange
                    if (heap.get(curr_pos) < heap.get(maxChild)){
                        int tmp = heap.get(curr_pos);
                        heap.set(curr_pos, heap.get(maxChild));
                        heap.set(maxChild, tmp);
                        curr_pos = maxChild;
                    }else {
                        break;
                    }

                }
            }
        }
    }

}
